<template>
  <div v-if="visible" class="enum-modal-overlay" @click.self="handleClose">
    <div class="enum-modal-container">
      <div class="enum-modal-header">
        <h3>Java枚举 - {{ enumName }}</h3>
        <button class="close-btn" @click="handleClose">&times;</button>
      </div>
      
      <div class="enum-modal-body">
        <div v-if="loading" class="loading">
          <div class="spinner"></div>
          <span>正在加载枚举数据...</span>
        </div>
        
        <div v-else-if="error" class="error-message">
          <span class="error-icon">⚠️</span>
          {{ error }}
        </div>
        
        <div v-else-if="enumData.length > 0" class="enum-table-wrapper">
          <div class="enum-info">
            <span class="enum-count">共 {{ enumData.length }} 个枚举值</span>
            <span class="enum-name-label">{{ enumName }}</span>
          </div>
          
          <table class="enum-table">
            <thead>
              <tr>
                <th class="col-value">值 (Value)</th>
                <th class="col-label">标签 (Label)</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in enumData" :key="item.value">
                <td class="col-value">{{ item.value }}</td>
                <td class="col-label">{{ item.label }}</td>
              </tr>
            </tbody>
          </table>
        </div>
        
        <div v-else class="no-data">
          <div class="no-data-icon">📊</div>
          <div class="no-data-text">暂无枚举数据</div>
          <div class="no-data-desc">枚举类 {{ enumName }} 下没有找到相关数据</div>
        </div>
      </div>
      
      <div class="enum-modal-footer">
        <button class="btn btn-close" @click="handleClose">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'EnumDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    enumName: {
      type: String,
      required: true
    },
    enumData: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    error: {
      type: String,
      default: null
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    }
  },
  mounted() {
    // 添加ESC键关闭功能
    const handleEsc = (e) => {
      if (e.key === 'Escape' && this.visible) {
        this.handleClose()
      }
    }
    document.addEventListener('keydown', handleEsc)
    this.$once('hook:beforeDestroy', () => {
      document.removeEventListener('keydown', handleEsc)
    })
  }
}
</script>

<style scoped>
.enum-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2100;
  backdrop-filter: blur(2px);
}

.enum-modal-container {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 700px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.enum-modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: white;
}

.enum-modal-header h3 {
  margin: 0;
  font-size: 1.3em;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 28px;
  color: white;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: background 0.2s;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.enum-modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1;
  min-height: 200px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #6b7280;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e5e7eb;
  border-top-color: #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.error-message {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px;
  background: #fee2e2;
  color: #dc2626;
  border-radius: 8px;
  border: 1px solid #fecaca;
}

.error-icon {
  font-size: 20px;
}

.enum-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding: 12px 16px;
  background: #eff6ff;
  border-radius: 8px;
  border: 1px solid #dbeafe;
}

.enum-count {
  font-weight: 600;
  color: #374151;
}

.enum-name-label {
  color: #3b82f6;
  font-size: 14px;
  font-family: 'Courier New', monospace;
}

.enum-table-wrapper {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
}

.enum-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.enum-table thead {
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
}

.enum-table th {
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  color: #374151;
  border-bottom: 2px solid #d1d5db;
}

.enum-table td {
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  vertical-align: top;
}

.enum-table tbody tr:hover {
  background: #f9fafb;
}

.enum-table tbody tr:nth-child(even) {
  background: #eff6ff;
}

.enum-table tbody tr:nth-child(even):hover {
  background: #e0f2fe;
}

/* 列宽控制 */
.col-value {
  width: 30%;
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: #3b82f6;
}

.col-label {
  width: 70%;
}

.no-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #9ca3af;
}

.no-data-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.no-data-text {
  font-size: 18px;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 8px;
}

.no-data-desc {
  font-size: 14px;
  color: #9ca3af;
}

.enum-modal-footer {
  padding: 16px 24px;
  border-top: 1px solid #e5e7eb;
  display: flex;
  justify-content: flex-end;
  background: #f9fafb;
}

.btn {
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.btn-close {
  background: #6b7280;
  color: white;
}

.btn-close:hover {
  background: #4b5563;
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .enum-modal-container {
    width: 95%;
    max-height: 95vh;
  }
  
  .enum-modal-header {
    padding: 16px 20px;
  }
  
  .enum-modal-header h3 {
    font-size: 1.1em;
  }
  
  .enum-modal-body {
    padding: 16px 20px;
  }
  
  .enum-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .enum-table {
    font-size: 12px;
  }
  
  .enum-table th,
  .enum-table td {
    padding: 8px 12px;
  }
  
  /* 移动端表格滚动 */
  .enum-table-wrapper {
    overflow-x: auto;
  }
  
  .enum-table {
    min-width: 500px;
  }
}
</style>